package com.rjhch.tonglink.client.base;

import com.tongtech.tlq.base.*;

public class CliMoni extends Thread{
    private String myQcuName;
    private String myQueName;
    private String myMsgType;
    private String myFileName;
    private TlqConnContext tlqConnContext = null;
    private TlqConnection tlqConnection = null;
    private TlqQCU tlqQcu = null;

    static int id = 0;
    private int flag=0;
    private int mySOrG=0;
    private int myIfCancle=0;

    public CliMoni(String QcuName, String QName, String MsgType,
                      String FileName, int sendOrget, int ifCancle) throws TlqException {
        myQcuName = QcuName;
        myQueName = QName;
        myMsgType = MsgType;
        myFileName = FileName;
        mySOrG = sendOrget;
        myIfCancle = ifCancle;

        tlqConnContext = new TlqConnContext();
        tlqConnContext.BrokerId = 111; //根据 tlqcli.conf中的配置

        /* tlqConnContext.BrokerId = -1;
         tlqConnContext.FilesDir= "D:\\TLQCli8\\files";
         tlqConnContext.HostName = "168.1.90.33";
         tlqConnContext.ListenPort = 10251;*/

        tlqConnection = new TlqConnection(tlqConnContext);
        tlqQcu = tlqConnection.openQCU(myQcuName);
        CliMoni moni = new CliMoni(tlqConnection,tlqQcu, myIfCancle);
        moni.start();

    }
     public CliMoni(TlqConnection tlqConnection,
                      TlqQCU tlqQcu, int ifCancle) throws TlqException {
       this.tlqConnection=tlqConnection;
       this.tlqQcu=tlqQcu;
       this.flag=1;
       this.myIfCancle = ifCancle;

    }
    public void run(){
        if(flag==1)
            MoniMsg();
        else
        {
            if(0 == mySOrG)
                sendMsg();
            else
                getMsg();
        }
    }
    
    public static synchronized String createID() {
        StringBuffer sb = new StringBuffer();
        sb.append(System.currentTimeMillis());
        sb.append("_");
        sb.append(id);
        ++id;
        return sb.toString();
    }

    public static byte[] createBytes(int size) { //构造Buffer消息内容
        StringBuffer sb = new StringBuffer(size);
        for (int i = 0; i < size; i++) {
            sb.append('a');
        }
        return sb.toString().getBytes();
    }
    
    
    
    void MoniMsg() { //监控消息发送和取消
        TlqTransInfo tlqTransInfo = new TlqTransInfo();
        
        System.out.println("-----------cancleTransfer-----------"+ myIfCancle);
        try
        {
            Thread.sleep(2000);
            System.out.println("-----------getTrans!!-----------");
            while(true)
            {
                int ret = tlqQcu.getTransProgress(tlqTransInfo);
                if(ret < 0 )
                {
                    System.out.println("-----------getTransProgress EE [" +ret +"]!!-----------");
                    break;
                }
                else if(0 == ret)
                {
                    System.out.println("-----------getTransProgress [" + tlqTransInfo.percent + "]!!-----------");
                    if(myIfCancle ==1 && tlqTransInfo.percent > 50 )
                    {
                        System.out.println("-----------cancleTransfer-----------");
                        ret = tlqQcu.cancleTransfer(tlqTransInfo, 111);
                        if(ret < 0 )
                        {
                            System.out.println("-----------cancleTransfer error!!-----------");
                            break;
                        }
                        else
                        {
                            System.out.println("-----------cancleTransfer success!!-----------");
                            break;
                        }
                    }
                }
                else
                {
                    System.out.println("-----------getTransProgress error [" + ret +"]!!-----------");
                    break;
                }
                Thread.sleep(2000);
            }
        }catch (Exception e)
        {
            e.printStackTrace();
        }
        
    }
    
    
    
    
    
    void sendBuffMsg(int size) throws TlqException { //发送Buffer消息 size 消息大小
        TlqMessage msgInfo = new TlqMessage();
        TlqMsgOpt msgOpt = new TlqMsgOpt();
        msgInfo.MsgType = TlqMessage.BUF_MSG; //消息类型
        msgInfo.MsgSize = size; //消息大小
        byte[] msgContent = createBytes(msgInfo.MsgSize); //消息内容
        msgInfo.setMsgData(msgContent);

        msgInfo.Persistence = TlqMessage.TLQPER_Y; //持久性
        msgInfo.Priority = TlqMessage.TLQPRI_NORMAL; //优先级
        msgInfo.Expiry = -1; //生命周期
        msgOpt.QueName = myQueName; //队列名

        tlqQcu.retransfer(msgInfo, msgOpt);
        tlqQcu.putMessage(msgInfo, msgOpt);
    }

    void sendFileMsg(String fName) throws TlqException { //发送文件消息
        TlqMessage msgInfo = new TlqMessage();
        TlqMsgOpt msgOpt = new TlqMsgOpt();

        msgInfo.MsgType = TlqMessage.FILE_MSG;

        fName = myFileName + " " + createID() + "_" + myFileName;
        msgInfo.MsgSize = fName.getBytes().length;
        msgInfo.setMsgData(fName.getBytes());
        msgOpt.RemoveFileFlag = TlqMsgOpt.NOTREMOVEFILE; //是否删除源文件标志

        msgInfo.Persistence = TlqMessage.TLQPER_Y;
        msgInfo.Priority = TlqMessage.TLQPRI_NORMAL;
        msgInfo.Expiry = -1;
        msgOpt.QueName = myQueName;

        tlqQcu.putMessage(msgInfo, msgOpt);
    }

    public void sendMsg() {
        System.out.println("-----SEND-----");
        try {
            for (int i = 0; i < 1; i++) {
                if (myMsgType.equals("B") == true) {
                    sendBuffMsg(1000);
                } else {
                    sendFileMsg(myFileName);
                }

            }
            System.out.println("-----------sendmsg over!!-----------");

        } catch (TlqException tlqEx) {
            tlqEx.printStackTrace();
        } /*finally {
            try {
                tlqQcu.close();
                tlqConnection.close();
            } catch (TlqException e) {
                e.printStackTrace();
            }
        }*/
    }
    
    public void getMsg() {
        TlqMessage msgInfo = new TlqMessage();
        TlqMsgOpt msgOpt = new TlqMsgOpt();
        
        msgOpt.QueName = myQueName;
        msgOpt.WaitInterval = 0;
        msgOpt.OperateType = TlqMsgOpt.TLQOT_GET;
        msgInfo.TransCancleflag = 1;
        
                
        System.out.println("-----GET-----"+ msgOpt.QueName);
        
        try {
            tlqQcu.retransfer(msgInfo, msgOpt);
            
            msgOpt.QueName = myQueName;
            msgOpt.WaitInterval = 0;
            msgOpt.AckMode=TlqMsgOpt.TLQACK_AUTO;
            msgOpt.OperateType = TlqMsgOpt.TLQOT_GET;
            msgInfo.TransCancleflag = 1;
        
            for (int i = 0; i < 1; i++) {
                tlqQcu.getMessage(msgInfo, msgOpt);

            }

        } catch (TlqException tlqEx) {
            tlqEx.printStackTrace();
        } /*finally {
            try {
                tlqQcu.close();
                tlqConnection.close();
            } catch (TlqException e) {
                e.printStackTrace();
            }
        }*/
        
        System.out.println("-----GET OVER-----");
    }

    public static void main(String[] argv) throws Exception {
        String QcuName;
        String QName;
        String FileName;
        String MsgType;
        int    sendOrGet;
        int    ifCancle;        

        if (argv.length < 1) {
            System.out.println("---------请输入参数！---------");
            System.out.println(
                    "---------注：如果消息类型为B,则FileName输入为no！---------");
            System.out.println(
                    "CliMoni QcuName QueName MsgType(B/F) FileName SendOrGet(0:Send;1:Get) ifCancle(0:no;1:yes)");
            return;

        }
        if (argv.length != 6) {
            System.out.println("---------您输入的参数格式不对，请重新输入！---------");
            System.out.println(
                    "---------注：如果消息类型为B,则FileName输入为no！---------");
            System.out.println(
                    "CliMoni QcuName QueName MsgType(B/F) FileName SendOrGet(0:Send;1:Get) ifCancle(0:no;1:yes)");
            return;
        } else {
            QcuName = argv[0];
            QName = argv[1];
            MsgType = argv[2];
            FileName = argv[3];
            sendOrGet = Integer.parseInt(argv[4]);
            ifCancle = Integer.parseInt(argv[5]);
        }
        CliMoni sm = new CliMoni(QcuName, QName, MsgType, FileName, sendOrGet, ifCancle);
        sm.start();

    }
}
